//
//  Migration.swift
//  hello
//
//  Created by 赵康 on 2025/3/30.
//

import Fluent
extension UserReviewSettings {
    struct Migration: AsyncMigration {
        var name: String { "CreateUserReviewSettings" }
        
        func prepare(on database: Database) async throws {
            try await database.schema(SchemaEnum.userReviewSettings.rawValue)
                .id()
                .field(FieldKeys.user, .uuid, .references(SchemaEnum.users.rawValue, .id, onDelete: .cascade))
                .field(FieldKeys.dailyReviewCount, .int, .required, .sql(.default(20)))
                .field(FieldKeys.prioritizeRecent, .bool, .required, .sql(.default(true)))
                .field(FieldKeys.lastSyncDate, .datetime)
                
                .field(FieldKeys.createdAt, .datetime)
                .field(FieldKeys.updatedAt, .datetime)
                .unique(on: FieldKeys.user)
                .create()
        }
        
        func revert(on database: Database) async throws {
            try await database.schema(SchemaEnum.userReviewSettings.rawValue).delete()
        }
    }
}

  // AddAnkiFieldsToUserReviewSettings.swift
extension UserReviewSettings {
  struct AddAnkiFieldsMigration: AsyncMigration {
    var name: String { "AddAnkiFieldsToUserReviewSettings" }
    
    func prepare(on database: Database) async throws {
      try await database.schema(SchemaEnum.userReviewSettings.rawValue)
        .field(FieldKeys.newCardsPerDay, .int, .required, .sql(.default(20)))
        .field(FieldKeys.learningSteps, .array(of: .int), .required) // 移除默认值
        .field(FieldKeys.maxReviewsPerDay, .int, .required, .sql(.default(100)))
        .field(FieldKeys.intervalModifier, .double, .required, .sql(.default(1.0)))
        .field(FieldKeys.maxInterval, .int, .required, .sql(.default(36500)))
        .field(FieldKeys.lapseSteps, .array(of: .int), .required) // 移除默认值
        .field(FieldKeys.lapseNewInterval, .double, .required, .sql(.default(0.5)))
        .field(FieldKeys.leechThreshold, .int, .required, .sql(.default(8)))
        .update()
    }
    
    func revert(on database: Database) async throws {
      try await database.schema(SchemaEnum.userReviewSettings.rawValue)
        .deleteField(FieldKeys.newCardsPerDay)
        .deleteField(FieldKeys.learningSteps)
        .deleteField(FieldKeys.maxReviewsPerDay)
        .deleteField(FieldKeys.intervalModifier)
        .deleteField(FieldKeys.maxInterval)
        .deleteField(FieldKeys.lapseSteps)
        .deleteField(FieldKeys.lapseNewInterval)
        .deleteField(FieldKeys.leechThreshold)
        .update()
    }
  }
}
